home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / wot-20080519-fx.xpi / chrome / wot.jar / content / settings.js < prev    next >
Text File  |  2008-02-13  |  7KB  |  322 lines

  1. /*
  2.     settings.js
  3.  
  4.     Copyright ┬⌐ 2007  Against Intuition, Inc. <info@mywot.com>
  5. */
  6.  
  7. var wot_settings =
  8. {
  9.     init: function()
  10.     {
  11.         try {
  12.             window.addEventListener("load", function(e) {
  13.                     wot_settings.load();
  14.                 }, false);
  15.             window.addEventListener("unload", function(e) {
  16.                     wot_settings.unload();
  17.                 }, false);
  18.         } catch (e) {
  19.             dump("wot_settings.init: failed with " + e + "\n");
  20.         }
  21.     },
  22.  
  23.     load: function()
  24.     {
  25.         try {
  26.             if (this.browser) {
  27.                 return;
  28.             }
  29.  
  30.             /* Hook up event handlers */
  31.             this.browser = document.getElementById("appcontent");
  32.  
  33.             if (this.browser) {
  34.                 this.browser.addEventListener("DOMContentLoaded",
  35.                     wot_settings.onload, false);
  36.             }
  37.         } catch (e) {
  38.             dump("wot_settings.load: failed with " + e + "\n");
  39.         }
  40.     },
  41.  
  42.     unload: function()
  43.     {
  44.         try {
  45.             if (this.browser) {
  46.                 this.browser.removeEventListener("DOMContentLoaded",
  47.                     wot_settings.onload, false);
  48.                 this.browser = null;
  49.             }
  50.         } catch (e) {
  51.             dump("wot_settings.unload: failed with " + e + "\n");
  52.         }
  53.     },
  54.  
  55.     onload: function(event)
  56.     {
  57.         try {
  58.             if (!event) {
  59.                 return;
  60.             }
  61.  
  62.             var content = event.originalTarget;
  63.  
  64.             if (!content || !content.location || !content.location.href ||
  65.                     content.location.href.indexOf(WOT_PREF_URL) != 0) {
  66.                 return;
  67.             }
  68.  
  69.             if (!wot_settings.loadinputs(content) ||
  70.                 !wot_settings.loadsearch(content)) {
  71.                 return;
  72.             }
  73.  
  74.             var save = content.getElementById("wotsave");
  75.  
  76.             if (!save) {
  77.                 return;
  78.             }
  79.  
  80.             save.addEventListener("click", function(e) {
  81.                         wot_settings.onsave(content, e);
  82.                     }, false);
  83.  
  84.             var level = content.getElementById("wotlevel");
  85.  
  86.             if (level) {
  87.                 if (wot_crypto.islevel("registered")) {
  88.                     level.setAttribute("level", "registered");
  89.                 }
  90.                 /* Other levels? */
  91.             }
  92.  
  93.             wot_search.addscript(content, "wotsettings_ready();");
  94.         } catch (e) {
  95.             dump("wot_settings.onload: failed with " + e + "\n");
  96.         }
  97.     },
  98.  
  99.     onsave: function(content, event)
  100.     {
  101.         try {
  102.             var save = content.getElementById("wotsave");
  103.  
  104.             if (save) {
  105.                 var saveclass = save.getAttribute("class");
  106.  
  107.                 if (saveclass && saveclass.indexOf("disabled") >= 0) {
  108.                     return;
  109.                 }
  110.             }
  111.  
  112.             var inputs = content.getElementsByTagName("input");
  113.  
  114.             for (var i = 0; i < inputs.length; ++i) {
  115.                 var preftype = inputs[i].getAttribute("wotpref");
  116.  
  117.                 if (!preftype) {
  118.                     continue;
  119.                 }
  120.                 
  121.                 var id = inputs[i].getAttribute("id");
  122.  
  123.                 if (!id) {
  124.                     continue;
  125.                 }
  126.  
  127.                 var type = inputs[i].getAttribute("type");
  128.  
  129.                 if (!type) {
  130.                     continue;
  131.                 }
  132.  
  133.                 if ((type == "checkbox" || type == "radio") &&
  134.                         preftype == "bool") {
  135.                     if (!wot_prefs.setBool(id, inputs[i].checked)) {
  136.                         dump("wot_settings.onsave: setBool failed for " +
  137.                             id + "\n");
  138.                     }
  139.                 } else {
  140.                     var value = inputs[i].getAttribute("value");
  141.  
  142.                     if (!value) {
  143.                         if (preftype == "string") {
  144.                             value = "";
  145.                         } else {
  146.                             dump("wot_settings.onsave: no value for " + id + "\n");
  147.                             continue;
  148.                         }
  149.                     }
  150.  
  151.                     if (preftype == "bool") {
  152.                         if (!wot_prefs.setBool(id, (value == "true"))) {
  153.                             dump("wot_settings.onsave: setBool failed for " +
  154.                                 id + "\n");
  155.                         }
  156.                     } else if (preftype == "int") {
  157.                         if (!wot_prefs.setInt(id, Number(value))) {
  158.                             dump("wot_settings.onsave: setInt failed for " +
  159.                                 id + " and value " + value + "\n");
  160.                         }
  161.                     } else if (preftype == "string") {
  162.                         if (!wot_prefs.setChar(id, value)) {
  163.                             dump("wot_settings.onsave: setChar failed for " +
  164.                                 id + "\n");
  165.                         }
  166.                     }
  167.                 }
  168.             }
  169.             wot_search.addscript(content, "wotsettings_saved();");
  170.             return;
  171.         } catch (e) {
  172.             dump("wot_settings.onsave: failed with " + e + "\n");
  173.         }
  174.         try {
  175.             wot_search.addscript(content, "wotsettings_failed();");
  176.         } catch (e) {
  177.             dump("wot_settings.onsave: failed with " + e + "\n");
  178.         }
  179.     },
  180.  
  181.     loadinputs: function(content)
  182.     {
  183.         try {
  184.             var inputs = content.getElementsByTagName("input");
  185.  
  186.             for (var i = 0; i < inputs.length; ++i) {
  187.                 var preftype = inputs[i].getAttribute("wotpref");
  188.  
  189.                 if (!preftype) {
  190.                     continue;
  191.                 }
  192.                 
  193.                 var id = inputs[i].getAttribute("id");
  194.  
  195.                 if (!id) {
  196.                     continue;
  197.                 }
  198.             
  199.                 var type = inputs[i].getAttribute("type");
  200.  
  201.                 if (!type) {
  202.                     continue;
  203.                 }
  204.                 
  205.                 var value = null;
  206.  
  207.                 if (preftype == "bool") {
  208.                     value = wot_prefs.getBool(id, null);
  209.                 } else if (preftype == "int") {
  210.                     value = wot_prefs.getInt(id, null);
  211.                 } else if (preftype == "string") {
  212.                     value = wot_prefs.getChar(id, null);
  213.                 } else {
  214.                     dump("wot_settings.loadinputs: invalid preftype " +
  215.                         preftype + "\n");
  216.                     continue;
  217.                 }
  218.  
  219.                 if (value == null) {
  220.                     dump("wot_settings.loadinputs: invalid pref " + id + "\n");
  221.                     continue;
  222.                 }
  223.  
  224.                 if ((type == "checkbox" || type == "radio") &&
  225.                         preftype == "bool") {
  226.                     inputs[i].checked = value;
  227.                 } else {
  228.                     inputs[i].setAttribute("value", value.toString());
  229.                 }
  230.             }
  231.             return true;
  232.         } catch (e) {
  233.             dump("wot_settings.loadinputs: failed with " + e + "\n");
  234.         }
  235.         return false;
  236.     },
  237.  
  238.     loadsearch: function(content)
  239.     {
  240.         try {
  241.             var search = content.getElementById("wotsearch");
  242.  
  243.             if (!search) {
  244.                 return true;
  245.             }
  246.  
  247.             var preftype = search.getAttribute("wotpref");
  248.  
  249.             if (!preftype || preftype != "input") {
  250.                 return false;
  251.             }
  252.  
  253.             var rules = new Array();
  254.  
  255.             var j = 0;
  256.  
  257.             for (var i in wot_search.rules) {
  258.                 if (!wot_search.rules[i].display ||
  259.                     !wot_search.rules[i].display.length) {
  260.                     continue;
  261.                 }
  262.                 rules[j++] = wot_search.rules[i].display;
  263.             }
  264.             rules.sort();
  265.  
  266.             for (j = 0; j < rules.length; ++j) {
  267.                 for (var i in wot_search.rules) {
  268.                     if (wot_search.rules[i].display != rules[j]) {
  269.                         continue;
  270.                     }
  271.                 
  272.                     var id = WOT_SEARCH + "." + wot_search.rules[i].rule +
  273.                                 "." + WOT_SEARCH_ENABLED;
  274.  
  275.                     var input = content.createElement("input");
  276.  
  277.                     if (!input) {
  278.                         break;
  279.                     }
  280.  
  281.                     input.setAttribute("id", id);
  282.                     input.setAttribute("type", "checkbox");
  283.                     input.setAttribute("wotpref", "bool");
  284.                     input.checked = wot_search.rules[i].enabled;
  285.  
  286.                     var label = content.createElement("label");
  287.  
  288.                     if (!label) {
  289.                         break;
  290.                     }
  291.  
  292.                     label.setAttribute("for", id);
  293.  
  294.                     var text = content.createTextNode(wot_search.rules[i].display);
  295.  
  296.                     if (!text) {
  297.                         break;
  298.                     }
  299.                 
  300.                     var br = content.createElement("br");
  301.  
  302.                     if (!br) {
  303.                         break;
  304.                     }
  305.                 
  306.                     label.appendChild(text);
  307.                     search.appendChild(input);
  308.                     search.appendChild(label);
  309.                     search.appendChild(br);
  310.                 }
  311.             }
  312.             return true;
  313.         } catch (e) {
  314.             dump("wot_settings.loadsearch: failed with " + e + "\n");
  315.         }
  316.         return false;
  317.     }
  318.         
  319. };
  320.  
  321. wot_settings.init();
  322.